package com.kinglone.common.utils;


import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.ConnectException;

/**
 * @Author:
 * @Description: openoffice的调用工具类
 * @Date: Created in
 * @Modified By:
 */
public class OpenOfficeUtil {
    private static Logger log = LoggerFactory.getLogger(OpenOfficeUtil.class);
    public static void docToPdf(String path1,String path2) {
        //System.out.println("docToPdf(String path1,String path2)---" + new Date());
        File inputFile = new File(path1);
        File outputFile = new File(path2);
        if (outputFile.exists()) return;

//		        OfficeManager officeManager = null;
//				DocumentConverter documentConverter = null;
//				LocalOfficeManager.Builder builder = LocalOfficeManager.builder();
//				System.out.println("LocalOfficeManager.builder()---" + new Date());
//				builder.officeHome("/opt/openoffice4");
//				//builder.officeHome("C/://Program Files (x86)/OpenOffice 4");
//				builder.portNumbers(8100);
//				officeManager = builder.build();
//				System.out.println("officeManager = builder.build();---" + new Date());
//				try
//				{
//					officeManager.start();
//					System.out.println("officeManager.start();---" + new Date());
//					documentConverter = LocalConverter.make(officeManager);
//					System.out.println("LocalConverter.make(officeManager);---" + new Date());
//					documentConverter.convert(inputFile).to(outputFile).execute();
//					System.out.println("documentConverter.convert;---" + new Date());
//				}
//				catch (OfficeException e)
//				{
//					e.printStackTrace();
//				}
//				finally
//				{
//					try
//					{
//						officeManager.stop();
//						System.out.println("officeManager.stop();");
//					}
//					catch (OfficeException e)
//					{
//						e.printStackTrace();
//					}
//				}

//        OpenOfficeConnection connection = new SocketOpenOfficeConnection("192.168.1.47",8100);
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100);
        //System.out.println("OpenOfficeConnection connection = new SocketOpenOfficeConnection(127.0.0.1,8100)---" + new Date());
        try {
            long starttime=System.currentTimeMillis();
            connection.connect();
            long endtime =System.currentTimeMillis();
            log.error("connection.connect()---" + (endtime-starttime)+" ms");
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);
        } catch (ConnectException cex) {
            cex.printStackTrace();
        }  catch (Exception cex) {
            cex.printStackTrace();
        }catch(Throwable e){
            e.printStackTrace();
        }finally {
            // close the connection
            if (connection != null) {
                connection.disconnect();
                connection = null;
            }
        }
    }

}
